package com.dlc.felear.xiaoensale.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.customView.LoadingDialog;
import com.dlc.felear.xiaoensale.deviceUtil.TopbarHelper;
import com.dlc.felear.xiaoensale.deviceControlSf.NewDeviceControl;
import com.itdlc.android.library.base.BaseActivity;

import org.simple.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 寸劲伺服电机手动控制
 */
public class SfMotorDebugActivity extends BaseActivity {
    private static final String TAG = "SfMotorDebugActivity";
    private static final int WHAT_CANCEL_DIALOG=2321;
    @BindView(R.id.et_speed)
    EditText etSpeed;
    @BindView(R.id.et_plus)
    EditText etPlus;
    @BindView(R.id.et_a1)
    EditText etNum;
    private TopbarHelper topbarHelper;
    private LoadingDialog loadingDialog;
    private NewDeviceControl control;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_step_m_sf;
    }

    @Override
    protected void initialView() {
        EventBus.getDefault().register(this);
        loadingDialog=new LoadingDialog(this,"正在执行..");
        topbarHelper = new TopbarHelper(this);
        topbarHelper.setTitle("伺服电机手动控制");
        topbarHelper.setcon(R.drawable.arrow_left);
        topbarHelper.setNavActivity(MotorPagerActivity.class);
        control=NewDeviceControl.getDeviceControlHelper(this);
    }


    @OnClick({ R.id.btn_stop, R.id.btn_zz, R.id.btn_fz,R.id.btn_speed})
    public void onItemClick(View view) {
        switch (view.getId()){
            case R.id.btn_stop:
                stop();
                break;
            case R.id.btn_zz:
                zhuandong(1);
                break;
            case R.id.btn_fz:
                zhuandong(2);
                break;
            case R.id.btn_speed:
                setSpeed();
                break;

        }

    }



    private void setSpeed(){
        loadingDialog.show(500);
        control.setSfSpeed(1,getEtValue(etSpeed));
    }
    private void stop(){
        loadingDialog.show(500);
        control.stopSf(getEtValue(etNum),1);
    }

    private void zhuandong(int dir){
        loadingDialog.show(500);
        control.startSfOnce(getEtValue(etNum),dir,getEtValue(etPlus));
    }

    private int getEtValue(EditText et){
        String tempS=et.getText().toString().trim();
        if(!TextUtils.isEmpty(tempS)){
           return Integer.parseInt(tempS);
        }
        return 1;
    }
}